<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="起始机构id" prop="startAgencyId">
      <el-input v-model="dataForm.startAgencyId" placeholder="起始机构id"></el-input>
    </el-form-item>
    <el-form-item label="目的机构id" prop="endAgencyId">
      <el-input v-model="dataForm.endAgencyId" placeholder="目的机构id"></el-input>
    </el-form-item>
    <el-form-item label="作业状态，1为待执行（对应 待提货）、2为进行中（对应在途）、3为改派（对应 已交付）、4为已完成（对应 已交付）、5为已作废" prop="status">
      <el-input v-model="dataForm.status" placeholder="作业状态，1为待执行（对应 待提货）、2为进行中（对应在途）、3为改派（对应 已交付）、4为已完成（对应 已交付）、5为已作废"></el-input>
    </el-form-item>
    <el-form-item label="司机id" prop="driverId">
      <el-input v-model="dataForm.driverId" placeholder="司机id"></el-input>
    </el-form-item>
    <el-form-item label="运输任务id" prop="taskTransportId">
      <el-input v-model="dataForm.taskTransportId" placeholder="运输任务id"></el-input>
    </el-form-item>
    <el-form-item label="提货对接人" prop="startHandover">
      <el-input v-model="dataForm.startHandover" placeholder="提货对接人"></el-input>
    </el-form-item>
    <el-form-item label="交付对接人" prop="finishHandover">
      <el-input v-model="dataForm.finishHandover" placeholder="交付对接人"></el-input>
    </el-form-item>
    <el-form-item label="计划发车时间" prop="planDepartureTime">
      <el-input v-model="dataForm.planDepartureTime" placeholder="计划发车时间"></el-input>
    </el-form-item>
    <el-form-item label="实际发车时间" prop="actualDepartureTime">
      <el-input v-model="dataForm.actualDepartureTime" placeholder="实际发车时间"></el-input>
    </el-form-item>
    <el-form-item label="计划到达时间" prop="planArrivalTime">
      <el-input v-model="dataForm.planArrivalTime" placeholder="计划到达时间"></el-input>
    </el-form-item>
    <el-form-item label="实际到达时间" prop="actualArrivalTime">
      <el-input v-model="dataForm.actualArrivalTime" placeholder="实际到达时间"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          startAgencyId: '',
          endAgencyId: '',
          status: '',
          driverId: '',
          taskTransportId: '',
          startHandover: '',
          finishHandover: '',
          planDepartureTime: '',
          actualDepartureTime: '',
          planArrivalTime: '',
          actualArrivalTime: '',
          createTime: ''
        },
        dataRule: {
          startAgencyId: [
            { required: true, message: '起始机构id不能为空', trigger: 'blur' }
          ],
          endAgencyId: [
            { required: true, message: '目的机构id不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '作业状态，1为待执行（对应 待提货）、2为进行中（对应在途）、3为改派（对应 已交付）、4为已完成（对应 已交付）、5为已作废不能为空', trigger: 'blur' }
          ],
          driverId: [
            { required: true, message: '司机id不能为空', trigger: 'blur' }
          ],
          taskTransportId: [
            { required: true, message: '运输任务id不能为空', trigger: 'blur' }
          ],
          startHandover: [
            { required: true, message: '提货对接人不能为空', trigger: 'blur' }
          ],
          finishHandover: [
            { required: true, message: '交付对接人不能为空', trigger: 'blur' }
          ],
          planDepartureTime: [
            { required: true, message: '计划发车时间不能为空', trigger: 'blur' }
          ],
          actualDepartureTime: [
            { required: true, message: '实际发车时间不能为空', trigger: 'blur' }
          ],
          planArrivalTime: [
            { required: true, message: '计划到达时间不能为空', trigger: 'blur' }
          ],
          actualArrivalTime: [
            { required: true, message: '实际到达时间不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/aggregation/driverjob/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.startAgencyId = data.driverJob.startAgencyId
                this.dataForm.endAgencyId = data.driverJob.endAgencyId
                this.dataForm.status = data.driverJob.status
                this.dataForm.driverId = data.driverJob.driverId
                this.dataForm.taskTransportId = data.driverJob.taskTransportId
                this.dataForm.startHandover = data.driverJob.startHandover
                this.dataForm.finishHandover = data.driverJob.finishHandover
                this.dataForm.planDepartureTime = data.driverJob.planDepartureTime
                this.dataForm.actualDepartureTime = data.driverJob.actualDepartureTime
                this.dataForm.planArrivalTime = data.driverJob.planArrivalTime
                this.dataForm.actualArrivalTime = data.driverJob.actualArrivalTime
                this.dataForm.createTime = data.driverJob.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/aggregation/driverjob/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'startAgencyId': this.dataForm.startAgencyId,
                'endAgencyId': this.dataForm.endAgencyId,
                'status': this.dataForm.status,
                'driverId': this.dataForm.driverId,
                'taskTransportId': this.dataForm.taskTransportId,
                'startHandover': this.dataForm.startHandover,
                'finishHandover': this.dataForm.finishHandover,
                'planDepartureTime': this.dataForm.planDepartureTime,
                'actualDepartureTime': this.dataForm.actualDepartureTime,
                'planArrivalTime': this.dataForm.planArrivalTime,
                'actualArrivalTime': this.dataForm.actualArrivalTime,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
